package in.srain.cube.util;

import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtil {

    private static final String BLANK = "";

    private final static DateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss a");

    public static String formatDate(Date date) {
        return sDateFormat.format(date);
    }

    /**
     * 判断是否是手机号
     */
    public static boolean isMobile(String mobile) {
        Pattern p = Pattern.compile("\\+(9[976]\\d|8[987530]\\d|6[987]\\d|5[90]\\d|42\\d|3[875]\\d|2[98654321]\\d|9[8543210]|8[6421]|6[6543210]|5[87654321]|4[987654310]|3[9643210]|2[70]|7|1)\\d{1,14}$");
        Matcher m = p.matcher(mobile);
        return m.matches();
    }

    /**
     * 判断字符串是否是数字
     */
    public static boolean isNumber(String value) {
        return isInteger(value) || isDouble(value);
    }

    /**
     * 判断字符串是否是整数
     */
    public static Boolean isInteger(String value) {
        try {
            Integer.parseInt(value);
            return true;
        } catch (NumberFormatException e) {
            return false;
        }
    }

    /**
     * 判断字符串是否是浮点数
     */
    public static Boolean isDouble(String value) {
        try {
            Double.parseDouble(value);
            return value.contains(".");
        } catch (NumberFormatException e) {
            return false;
        }
    }

    public static boolean isAnyEmpty(String... strings) {
        for (String string : strings) {
            if (TextUtils.isEmpty(string)) {
                return true;
            }
        }
        return false;
    }

    public static boolean isAnyEmptyAfterTrim(String... strings) {
        for (String string : strings) {
            if (string == null || TextUtils.isEmpty(string.trim())) {
                return true;
            }
        }
        return false;
    }

    public static String blank() {
        return BLANK;
    }

    public static String blankIfNull(String s) {
        return s != null ? s : BLANK;
    }

    public static String defaultIfEmpty(String s, String fallback) {
        return !TextUtils.isEmpty(s) ? s : fallback;
    }

    public static String renderString(String doc, Map<String, Object> map) {
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            String string = "\\{\\$" + entry.getKey() + "\\}";
            doc = doc.replaceAll(string, String.valueOf(entry.getValue()));
        }
        return doc;
    }

    @NonNull
    public static String trim(@Nullable String s) {
        return s == null ? BLANK : s.trim();
    }

    public static int toInt(String str) {
        if (TextUtils.isEmpty(str)) {
            return 0;
        }
        try {
            return Integer.valueOf(str);
        } catch (Throwable e) {
            return 0;
        }
    }

    public static boolean toBooleanDefaultTrue(String str) {
        if (TextUtils.isEmpty(str)) {
            return true;
        }
        return Boolean.valueOf(str);
    }

    public static boolean contains(String s1, String s2) {
        return s1.contains(s2) || s2.contains(s1);
    }
}
